package com.mxpio.erp.sales.controller;


import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.sales.entity.Invoice;
import com.mxpio.erp.common.sales.service.InvoiceService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "InvoiceController", description = "销售发票接口")
@RestController
@RequestMapping("/erp/sales/invoice/")
public class InvoiceController {

    @Autowired
    private InvoiceService invoiceService;

    @GetMapping("list")
    @Operation(summary = "销售发票列表", description = "获取销售发票列表（无分页）", method = "GET")
    public Result<List<Invoice>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<Invoice> invoices = invoiceService.list(Invoice.class, criteria);
        return Result.OK(invoices);
    }

    @GetMapping("page")
    @Operation(summary = "销售发票列表", description = "获取销售发票列表（分页）", method = "GET")
    public Result<Page<Invoice>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<Invoice> invoices = invoiceService.listPage(Invoice.class, page, criteria);
        return Result.OK(invoices);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取销售发票", description = "根据bizNo获取销售发票", method = "GET")
    public Result<Invoice> getById(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Invoice invoice = invoiceService.getById(Invoice.class, bizNo);
        return Result.OK(invoice);
    }

    @PostMapping("add")
    @Operation(summary = "新增销售发票", description = "新增销售发票", method = "POST")
    public Result<Invoice> add(@RequestBody Invoice invoice) {
        invoiceService.save(invoice);
        return Result.OK(invoice);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑销售发票", description = "编辑销售发票", method = "PUT")
    public Result<Invoice> edit(@RequestBody Invoice invoice) {
        invoiceService.update(invoice);
        return Result.OK(invoice);
    }

    @DeleteMapping("remove/{bizNos}")
    @Operation(summary = "根据bizNo删除销售发票", description = "删除销售发票", method = "DELETE")
    public Result<Invoice> remove(@Parameter(description="业务单号，多个单号用英文逗号分隔") @PathVariable(name = "bizNos", required = true) String bizNos) {
        String[] bizNo = bizNos.split(",");
        for(String key : bizNo){
            invoiceService.delete(Invoice.class, key);
        }
        return Result.OK();
    }
}
